<template>
  <el-container>
    <el-header>
      <div>
        <img src="@/assets/logo.png" height="20px" alt="" />
        xxxx管理系统
      </div>
      <el-button type="primary" size="small" @click="logout">退出登录</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-col>
          <el-menu
            :router="true"
            class="el-menu-vertical-demo"
            background-color="#343744"
            text-color="#fff"
            active-text-color="#3F9EFF"
            :collapse="isCollapse"
            :collapse-transition="false"
            :default-active="activePath"
          >
            <el-submenu :index="menu.id + ''" v-for="menu in menus" v-bind:key="menu.id">
              <template slot="title">
                <i :class="iconMap[menu.id]"></i>
                <span>{{ menu.authName }}</span>
              </template>

              <el-menu-item :index="sub.path" v-for="sub in menu.children" v-bind:key="sub.id" @click="saveNavState(sub.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ sub.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-col>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMenus } from "@/api/rights";
import { GET_ACTIVE_PATH, SET_ACTIVE_PATH } from "@/util/storage";
export default {
  data() {
    return {
      iconMap: {
        125: "iconfont icon-user",
        103: "iconfont icon-quanxianmiyao",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-dingdan",
        145: "iconfont icon-tongji"
      },
      menus: [],

      // 是否折叠
      isCollapse: false,

      // 被激活的链接地址
      activePath: ""
    };
  },
  created() {
    getMenus().then(res => {
      console.log(res);
      if (res.meta.status !== 200) {
        this.$message.error("获取菜单错误");
        return;
      }
      this.menus = res.data;
    });
    this.activePath = GET_ACTIVE_PATH();
  },

  components: {},

  methods: {
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      SET_ACTIVE_PATH(activePath);
      this.activePath = activePath;
    },
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    }
  }
};
</script>

<style scoped>
.iconfont {
  margin-right: 10px;
}
.el-header {
  background-color: #373d42;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
  font-size: 18px;
}
.el-container {
  height: 100%;
}
.el-aside {
  background-color: #343744;
  height: 100%;
}
.el-menu {
  border: 0;
}
.el-main {
  background-color: #ebedf1;
}
.toggle-button {
  text-align: center;
  color: white;
  background-color: #4a5064;
  font-size: 12px;
  letter-spacing: 0.2em;

  line-height: 24px;
  cursor: pointer;
}
</style>
